<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="库存ID" prop="inventoryId">
        <el-input
          v-model="queryParams.inventoryId"
          placeholder="请输入库存ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="operationType">
        <el-select v-model="queryParams.operationType" placeholder="请选择操作类型" clearable size="small">
          <el-option
            v-for="dict in operationTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库ID" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="位置ID" prop="locationId">
        <el-input
          v-model="queryParams.locationId"
          placeholder="请输入位置ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="operationBy">
        <el-input
          v-model="queryParams.operationBy"
          placeholder="请输入操作人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作时间" prop="operationTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.operationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择操作时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备用" prop="remarkSpare">
        <el-input
          v-model="queryParams.remarkSpare"
          placeholder="请输入备用"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用1" prop="remarkSpare1">
        <el-input
          v-model="queryParams.remarkSpare1"
          placeholder="请输入备用1"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用2" prop="remarkSpare2">
        <el-input
          v-model="queryParams.remarkSpare2"
          placeholder="请输入备用2"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['warehouse:in:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['warehouse:in:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['warehouse:in:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['warehouse:in:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inoutList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="库存ID" align="center" prop="inventoryId" />
      <el-table-column label="操作类型" align="center" prop="operationType" :formatter="operationTypeFormat" />
      <el-table-column label="数量" align="center" prop="quantity" :formatter="quantityFormat" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" :formatter="warehouseIdFormat" />
      <el-table-column label="位置ID" align="center" prop="locationId" :formatter="locationIdFormat" />
      <el-table-column label="操作人" align="center" prop="operationBy" :formatter="operationByFormat" />
      <el-table-column label="操作时间" align="center" prop="operationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :formatter="remarkFormat" />
      <el-table-column label="备用" align="center" prop="remarkSpare" :formatter="remarkSpareFormat" />
      <el-table-column label="备用1" align="center" prop="remarkSpare1" :formatter="remarkSpare1Format" />
      <el-table-column label="备用2" align="center" prop="remarkSpare2" :formatter="remarkSpare2Format" />
      <el-table-column label="部门ID" align="center" prop="deptId" :formatter="deptIdFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:in:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['warehouse:in:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库出入库操作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库存ID" prop="inventoryId">
          <el-input v-model="form.inventoryId" placeholder="请输入库存ID" />
        </el-form-item>
        <el-form-item label="操作类型" prop="operationType">
          <el-select v-model="form.operationType" placeholder="请选择操作类型">
            <el-option
              v-for="dict in operationTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="位置ID" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入位置ID" />
        </el-form-item>
        <el-form-item label="操作人" prop="operationBy">
          <el-input v-model="form.operationBy" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="操作时间" prop="operationTime">
          <el-date-picker clearable size="small"
            v-model="form.operationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="备用" prop="remarkSpare">
          <el-input v-model="form.remarkSpare" placeholder="请输入备用" />
        </el-form-item>
        <el-form-item label="备用1" prop="remarkSpare1">
          <el-input v-model="form.remarkSpare1" placeholder="请输入备用1" />
        </el-form-item>
        <el-form-item label="备用2" prop="remarkSpare2">
          <el-input v-model="form.remarkSpare2" placeholder="请输入备用2" />
        </el-form-item>
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInout, getInout, delInout, addInout, updateInout, exportInout } from "@/api/warehouse/in";

export default {
  name: "Inout",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库出入库操作表格数据
      inoutList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 主键ID字典
      idOptions: [],
      // 操作类型字典
      operationTypeOptions: [],
      // 数量字典
      quantityOptions: [],
      // 仓库ID字典
      warehouseIdOptions: [],
      // 位置ID字典
      locationIdOptions: [],
      // 操作人字典
      operationByOptions: [],
      // 操作时间字典
      operationTimeOptions: [],
      // 备注字典
      remarkOptions: [],
      // 备用字典
      remarkSpareOptions: [],
      // 备用1字典
      remarkSpare1Options: [],
      // 备用2字典
      remarkSpare2Options: [],
      // 创建人字典
      createByOptions: [],
      // 创建时间字典
      createTimeOptions: [],
      // 部门ID字典
      deptIdOptions: [],
      // 修改人字典
      updateByOptions: [],
      // 修改时间字典
      updateTimeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inventoryId: null,
        operationType: null,
        quantity: null,
        warehouseId: null,
        locationId: null,
        operationBy: null,
        operationTime: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("${column.dictType}").then(response => {
      this.idOptions = response.data;
    });
    this.getDicts("warehouse_operation_type").then(response => {
      this.operationTypeOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.quantityOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.warehouseIdOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.locationIdOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.operationByOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.operationTimeOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.remarkOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.remarkSpareOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.remarkSpare1Options = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.remarkSpare2Options = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.createByOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.createTimeOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.deptIdOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.updateByOptions = response.data;
    });
    this.getDicts("${column.dictType}").then(response => {
      this.updateTimeOptions = response.data;
    });
  },
  methods: {
    /** 查询仓库出入库操作列表 */
    getList() {
      this.loading = true;
      listInout(this.queryParams).then(response => {
        this.inoutList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 主键ID字典翻译
    idFormat(row, column) {
      return this.selectDictLabel(this.idOptions, row.id);
    },
    // 操作类型字典翻译
    operationTypeFormat(row, column) {
      return this.selectDictLabel(this.operationTypeOptions, row.operationType);
    },
    // 数量字典翻译
    quantityFormat(row, column) {
      return this.selectDictLabel(this.quantityOptions, row.quantity);
    },
    // 仓库ID字典翻译
    warehouseIdFormat(row, column) {
      return this.selectDictLabel(this.warehouseIdOptions, row.warehouseId);
    },
    // 位置ID字典翻译
    locationIdFormat(row, column) {
      return this.selectDictLabel(this.locationIdOptions, row.locationId);
    },
    // 操作人字典翻译
    operationByFormat(row, column) {
      return this.selectDictLabel(this.operationByOptions, row.operationBy);
    },
    // 操作时间字典翻译
    operationTimeFormat(row, column) {
      return this.selectDictLabel(this.operationTimeOptions, row.operationTime);
    },
    // 备注字典翻译
    remarkFormat(row, column) {
      return this.selectDictLabel(this.remarkOptions, row.remark);
    },
    // 备用字典翻译
    remarkSpareFormat(row, column) {
      return this.selectDictLabel(this.remarkSpareOptions, row.remarkSpare);
    },
    // 备用1字典翻译
    remarkSpare1Format(row, column) {
      return this.selectDictLabel(this.remarkSpare1Options, row.remarkSpare1);
    },
    // 备用2字典翻译
    remarkSpare2Format(row, column) {
      return this.selectDictLabel(this.remarkSpare2Options, row.remarkSpare2);
    },
    // 创建人字典翻译
    createByFormat(row, column) {
      return this.selectDictLabel(this.createByOptions, row.createBy);
    },
    // 创建时间字典翻译
    createTimeFormat(row, column) {
      return this.selectDictLabel(this.createTimeOptions, row.createTime);
    },
    // 部门ID字典翻译
    deptIdFormat(row, column) {
      return this.selectDictLabel(this.deptIdOptions, row.deptId);
    },
    // 修改人字典翻译
    updateByFormat(row, column) {
      return this.selectDictLabel(this.updateByOptions, row.updateBy);
    },
    // 修改时间字典翻译
    updateTimeFormat(row, column) {
      return this.selectDictLabel(this.updateTimeOptions, row.updateTime);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inventoryId: null,
        operationType: null,
        quantity: null,
        warehouseId: null,
        locationId: null,
        operationBy: null,
        operationTime: null,
        remark: null,
        remarkSpare: null,
        remarkSpare1: null,
        remarkSpare2: null,
        createBy: null,
        createTime: null,
        deptId: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库出入库操作";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInout(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库出入库操作";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInout(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInout(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除仓库出入库操作编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInout(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有仓库出入库操作数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInout(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
